package com.corner.utils.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.RotateDrawable;
import android.net.Uri;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.corner.R;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.DraweeHolder;

/**
 * 方形图片
 * Created by Administrator on 2015/5/2.
 */
public class SquareImageView extends ImageView {

    DraweeHolder mDraweeHolder;
    private Drawable drawable;
    private float[] scale;
    private Paint paint;

    public SquareImageView(Context context) {
        super(context);
        init();
    }

    public SquareImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SquareImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mDraweeHolder.onDetach();
    }

    @Override
    public void onStartTemporaryDetach() {
        super.onStartTemporaryDetach();
        mDraweeHolder.onDetach();
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        mDraweeHolder.onAttach();
    }

    @Override
    public void onFinishTemporaryDetach() {
        super.onFinishTemporaryDetach();
        mDraweeHolder.onAttach();
    }

    private void init() {
        paint = new Paint();
        paint.setFlags(Paint.ANTI_ALIAS_FLAG);
        paint.setAntiAlias(true);

        RotateDrawable drawable = (RotateDrawable) getResources().getDrawable(R.drawable.square_img_progress);
        GenericDraweeHierarchy hierarchy = new GenericDraweeHierarchyBuilder(getResources())
                .setProgressBarImage(drawable, ScalingUtils.ScaleType.CENTER).build();
        hierarchy.setActualImageScaleType(ScalingUtils.ScaleType.FOCUS_CROP);
        mDraweeHolder = DraweeHolder.create(hierarchy, getContext());
    }

    /**
     * 设置uri
     *
     * @param uri
     */
    public void setImageUri(Uri uri) {
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setUri(uri)
                .setOldController(mDraweeHolder.getController())
                .build();
        mDraweeHolder.setController(controller);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawable = mDraweeHolder.getTopLevelDrawable();
        setImageDrawable(drawable);
        drawable.draw(canvas);
    }

    public void setScale(float[] scale) {
        if (scale == null) {
            return;
        }
        this.scale = scale;
        if (scale[0] < 0.01 && scale[1] < 0.01 && scale[2] > 0.99 && scale[3] > 0.99) {
            ((GenericDraweeHierarchy) mDraweeHolder.getHierarchy()).setActualImageScaleType(ScalingUtils.ScaleType.FIT_CENTER);
        } else {
            ((GenericDraweeHierarchy) mDraweeHolder.getHierarchy()).setActualImageScaleType(ScalingUtils.ScaleType.FOCUS_CROP);
            ((GenericDraweeHierarchy) mDraweeHolder.getHierarchy()).setActualImageFocusPoint(new PointF((scale[0] + scale[2]) / 2f, (scale[1] + scale[3]) / 2f));
        }
    }
}
